﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using M3G;

namespace MyTestProject {
    public class TestSampleLinear {

        /**
         * Linear補間、Constantリピートのテスト
         * */
        [Fact]
        public void TestLinearConstant () {
            var kseq = new KeyframeSequence (4, 1, KeyframeSequence.Linear);
            kseq.RepeatMode = KeyframeSequence.Constant;
            kseq.Duration = 500;

            // 有効範囲外は境界のキーフレーム(0 or 3)の値をそのまま使用する
            //                   time             value
            kseq.SetKeyframe (0, 100, new float[] { 1.0f });
            kseq.SetKeyframe (1, 200, new float[] { 2.0f });
            kseq.SetKeyframe (2, 300, new float[] { 3.0f });
            kseq.SetKeyframe (3, 400, new float[] { 4.0f });

            Assert.Equal (1.0f, kseq.Sample (-300, 0));
            Assert.Equal (1.0f, kseq.Sample (50, 0));
            Assert.Equal (1.0f, kseq.Sample (100, 0));
            Assert.Equal (1.5f, kseq.Sample (150, 0));
            Assert.Equal (2.0f, kseq.Sample (200, 0));
            Assert.Equal (4.0f, kseq.Sample (400, 0));
            Assert.Equal (4.0f, kseq.Sample (450, 0));
            Assert.Equal (4.0f, kseq.Sample (600, 0));
        }

        /**
         * Linear補間、Loopリピートのテスト
         * */
        [Fact]
        public void TestLinearLoop () {
            var kseq = new KeyframeSequence (4, 1, KeyframeSequence.Linear);
            kseq.RepeatMode = KeyframeSequence.Loop;
            kseq.Duration = 500;

            // キーフレーム3(time=400)から次のイテレーションの1(time=100)は
            // Durationが500なのでtime=+200, value=4->1
            // したがって傾きは-3/200.
            //                   time             value
            kseq.SetKeyframe (0, 100, new float[] { 1.0f });
            kseq.SetKeyframe (1, 200, new float[] { 2.0f });
            kseq.SetKeyframe (2, 300, new float[] { 3.0f });
            kseq.SetKeyframe (3, 400, new float[] { 4.0f });

            Assert.Equal (2.5f, kseq.Sample (-250, 0));
            Assert.Equal (1.75f, kseq.Sample (50, 0));
            Assert.Equal (1.0f, kseq.Sample (100, 0));
            Assert.Equal (1.5f, kseq.Sample (150, 0));
            Assert.Equal (2.0f, kseq.Sample (200, 0));
            Assert.Equal (4.0f, kseq.Sample (400, 0));
            Assert.Equal (3.25f, kseq.Sample (450, 0));
            Assert.Equal (1.0f, kseq.Sample (600, 0));
        }
    }
}
